package com.zk.babala.datasource;

import com.zk.babala.entity.CategoryInfo;
import com.zk.babala.entity.LineInfo;
import com.zk.babala.entity.Movie;
import com.zk.babala.entity.PlayNumInfo;
import com.zk.babala.entity.VideoInfo;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.LineUtil;
import com.zk.babala.util.RequestUtil;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DuanjuSource implements ISourceParse {
    private String nextUrl;
    private String searchUrl;

    @Override
    public Map<String, String> getSourceMeta() {
        Map<String, String> map = new HashMap<>();
        map.put("source", this.getClass().getSimpleName());
        map.put("name", "短剧网");
        map.put("url", "https://duanju2.com/");
        return map;
    }


    @Override
    public List<CategoryInfo> getCategory() {
        List<CategoryInfo> categoryInfoList = new ArrayList<>();
        categoryInfoList.add(new CategoryInfo("热播", "type/duanju.html"));
        categoryInfoList.add(new CategoryInfo("年代穿越", "show/duanju---年代穿越--------.html"));
        categoryInfoList.add(new CategoryInfo("古装仙侠", "show/duanju---古装仙侠--------.html"));
        categoryInfoList.add(new CategoryInfo("脑洞悬疑", "show/duanju---脑洞悬疑--------.html"));
        categoryInfoList.add(new CategoryInfo("爽文", "show/duanju---爽文--------.html"));
        searchUrl = "search/-------------.html?wd=";
        return categoryInfoList;
    }


    @Override
    public List<Movie> getMovies(String url) {
        List<Movie> list = new ArrayList<>();
        Document document = RequestUtil.get(url);
        Elements select = document.select(".entry-media");
//        if (select.isEmpty()) {
//            select = document.select(".sub-pic");
//        }
        if (select.isEmpty()) {
            RequestUtil.toast("未解析到视频内容");
            return list;
        }
        select.forEach(li -> {
            Element a = li.getElementsByTag("a").get(0);
            Movie m = new Movie();
            m.setTitle(a.attr("title"));
            m.setVideoUrl(LineUtil.getLineUtil().swapUrl(a.attr("href")));
            String cardImageUrl = a.getElementsByTag("img").get(0).attr("data-src");
            m.setCardImageUrl(LineUtil.getLineUtil().swapUrl(cardImageUrl));
            list.add(m);
        });
        nextUrl = null;
        Elements pageElement = document.select(".pagination a");
        if (!pageElement.isEmpty()) {
            pageElement.forEach(page -> {
                if (page.text().equals("下一页")) {
                    nextUrl = page.attr("href");
                }
            });
        }
        return list;
    }

    @Override
    public VideoInfo getVideoDetail(String url) {
        VideoInfo videoInfo = new VideoInfo();
        Document document = RequestUtil.get(url);
        Elements infoNodes = document.select(".pricing-options li");
        infoNodes.forEach(infoNode -> {
            videoInfo.getDetailInfoList().add(infoNode.text());
        });
        videoInfo.getDetailInfoList().add(document.getElementsByClass("modal-body").get(0).text());
        List<LineInfo> lineInfoList = new ArrayList<>();
        Elements playNodes = document.select(".nav-link");
        for (int i = 0; i < playNodes.size(); i++) {
            Element node = playNodes.get(i);
            if (node.text().equals("播放列表")) {
                continue;
            }
            List<PlayNumInfo> playItemList = new ArrayList<>();
            Elements playList = document.getElementsByClass("accordion").get(i - 1).select("a");
            for (int p = 0; p < playList.size(); p++) {
                Element item = playList.get(p);
                PlayNumInfo play = new PlayNumInfo();
                play.setId(p);
                play.setTitle(item.text());
                play.setPageUrl(item.attr("href"));
                playItemList.add(play);
            }

            LineInfo lineInfo = new LineInfo();
            lineInfo.setTitle(node.text());
            lineInfo.setPlayNumInfoList(playItemList);
            lineInfoList.add(lineInfo);
        }

        videoInfo.setLineInfoList(lineInfoList);
        return videoInfo;
    }

    @Override
    public String getPlayUrl(String url) {
        Document document = RequestUtil.get(url);
        String content = document.getElementsByClass("container-lg").get(0).toString();
        return LineUtil.getLineUtil().getPlayUrl(content);

    }

    @Override
    public String getSearchUrl() {
        return LineUtil.getLineUtil().getLine() + searchUrl;
    }


    @Override
    public List<Movie> search(String word) {
        String searchUrl = getSearchUrl() + word;
        nextUrl = null;
        return getMovies(searchUrl);
    }

    @Override
    public List<Movie> loadNext() {
        if (nextUrl == null) {
            return Collections.emptyList();
        } else {
            return getMovies(nextUrl);
        }
    }

}
